package service

import (
	"git.oschina.net/fanbuchi/xgggh/order/database"
	order "git.oschina.net/fanbuchi/xgggh/order/proto/order"
	product "git.oschina.net/fanbuchi/xgggh/product/proto/product"
	productCli "git.oschina.net/fanbuchi/xgggh/product/cli"
	"errors"
	"git.oschina.net/fanbuchi/xgggh/utils/page"
	"git.oschina.net/fanbuchi/xgggh/order/model"
)

type Order struct {

}

func (super *Order)Create(req *order.CreateReq) (*order.Order,error)  {
	if len(req.Products) == 0 {
		return nil,errors.New("product is not allowed null")
	}
	//1.根据商品id列表查询出来的商品信息
	productIds := make([]int64,len(req.Products))
	for _, product := range req.Products {
		productIds=append(productIds,product.Id)
	}
	queryListReq:=product.QueryListReq{Id:productIds}
	rsp,err:=productCli.QueryList(&queryListReq)
	if err!=nil {
		return nil,err
	}
	if !rsp.Ok {
		return nil,errors.New(rsp.ErrorDesc)
	}
	//价格
	var totalFee int64;
	//生成订单id Name-->等于第一个商品的名字 price-->根据商品算出来
	ordeReq:=order.Order{Name:"买会员",Fee:2,Status:order.OrderStatus_created}
	_,err= database.Master().InsertOne(&ordeReq)
	if err !=nil{
		return nil,err
	}
	// 生成订单-商品中间数据
	var orderProducts []*model.OrderProduct;
	for _, product := range rsp.Product {
		orderProduct:=model.OrderProduct{
			OrderId:ordeReq.Id,
			ProductId:product.Id,
		}
		//需要减少的库存量
		for _, reqProducts := range req.Products {
			if reqProducts.Id == product.Id{
				//更新需要的减少库存
				orderProduct.Stock=reqProducts.Stock
				//商品价格加起来
				totalFee+=int64(reqProducts.Stock)*product.Price
				//更新订单名称
				//ordeReq.Name+=strings.Join(product.GetName(),",")
				ordeReq.Name=product.GetName()
			}
		}
		orderProducts = append(orderProducts,&orderProduct)
	}
	//插入数据库 商品价格-优惠价格
	ordeReq.Fee=totalFee
	_,err=database.Master().Update(&ordeReq)
	_,err=database.Master().Insert(orderProducts)
	if err !=nil {
		return nil,err
	}
	return &ordeReq,err
}

//Update会自动从user结构体中提取非0和非nil得值作为需要更新的内容
//Cols("Desc") 更新自定字段
// affected, err := engine.Table(new(User)).Id(id).Update(map[string]interface{}{"age":0})
func (super *Order)Update(req *order.UpdateReq) error  {
	if req.Id <= 0 {
		err := errors.New("column Id required")
		return err
	}
	//只允许更新订单价格和订单状态
	order:=order.Order{Id:req.GetId(),Fee:req.GetFee(),Status:req.OrderStatus}
	affected,err:= database.Master().Id(req.Id).Update(order)
	if affected == 0 {
		err := errors.New("0 rows was updated")
		return err
	}
	return err
}

func (super *Order)Delete(req *order.DeleteReq) error  {
	if req.Id <= 0 {
		err := errors.New("column Id required")
		return err
	}
	tmp:=order.Order{Id:req.Id}
	clomns,err:= database.Master().Delete(&tmp)
	if clomns == 0 {
		err := errors.New("0 rows was deleted")
		return err
	}
	return err
}

func (super *Order)Query(req *order.QueryReq) ( *order.Order, error)  {
	if req.Id <= 0 {
		err := errors.New("column Id required")
		return nil,err
	}

	var tmp order.Order
	_,err:= database.Master().ID(req.Id).Unscoped().Get(&tmp)
	return &tmp,err
}

func (super *Order)Search(req *order.SearchReq) ([]*order.Order,error)  {
	limit,offset:=page.Page(req.PageNumber,req.ResultPerPage)
	params:=req.Params
	if params==nil {
		params=&order.Order{}
	}
	var tmp []*order.Order;
	err:= database.Master().Limit(limit,offset).Find(&tmp,params)
	return tmp,err
}
